package com.dmall.graph.bfs;

import com.dmall.graph.Graph;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: xiao1.wang
 * @date: 2022/6/19 15:30
 */
public class CC1 {
    private Graph G;
    private boolean[] visited;
    private int cccount = 0;

    public CC1(Graph G) {
        this.G = G;
        this.visited = new boolean[G.V()];
        for (int i = 0; i < G.V(); i++) {
            if (!visited[i]) {
                bfs(i);
                cccount ++;
            }
        }
    }

    private void bfs(int v) {
        visited[v] = true;
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(v);
        while (!queue.isEmpty()) {
            Integer cur = queue.poll();
            for (Integer w : G.adj(cur)) {
                if (!visited[w]) {
                    visited[w] = true;
                    queue.offer(w);
                }
            }
        }
    }

    public int count() {
        return cccount;
    }

    public static void main(String[] args) {
        String filename = GraphBFS.class.getClassLoader().getResource("g.txt").getPath();
        Graph g = new Graph(filename);
        CC1 cc = new CC1(g);
        System.out.println(cc.count());
    }
}
